package com.scy.java.io.output;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

//图片复制
public class CopyPicture {
    public static void main(String[] args) {
        //创建流对象
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            //指定数据源
            fis = new FileInputStream("d:/apple.png");
            //指定目的地
            fos = new FileOutputStream("test_copy.png");
            //读写数据
            //定义字节数组
            byte[] bytes = new byte[1024];
            //定义长度
            int len;
            //循环读写
            while ((len = fis.read(bytes))!= -1){
                //循环写出数据
                fos.write(bytes,0,len);
            }
            System.out.println("复制完毕");

        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            //关闭资源
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
